-- golua

local km = require("km")
local json = require("json")
local ioutil = require("ioutil")


-- local err = km.gzip("tests/diff.json","tests/diff.json.gz")
-- if err then error(err) end

-- local err = km.gunzip("tests/diff.json.gz","tests/diff.json.ungz")
-- if err then error(err) end

-- end


local res = km.walk("public/")

local obj = {}
local k = 1

for i,v in ipairs(res) do
    if not v.isDir then
        local newVal = {
            path = v.path,
            size = v.size,
            -- name = v.name,
            -- isDir = v.isDir,
            md5 = km.md5(v.path),
        }
        obj[k] = newVal
        k = k + 1
    end
end

local md5List = ""
for i,v in ipairs(obj) do
    md5List = md5List .. v.md5 .. ' ' .. v.path .. '\n'
end

local oldMd5ListStr, err = ioutil.read_file("kkbt/diff.old.txt")
if err then
    print(err) 
    oldMd5ListStr = ""
end

local newLines = {}
for line in md5List:gmatch("(.-)\n") do
    table.insert(newLines, line)
end

-- local oldLines = {}
-- for line in oldMd5ListStr:gmatch("(.-)\n") do
--     table.insert(oldLines, line)
-- end


local uniqueLines1 = {}

for _, line in ipairs(newLines) do
    -- 如果这一行不在str2中，则添加到uniqueLines1中
    if not strings:Contains(oldMd5ListStr, line) then
        table.insert(uniqueLines1, line)
    end
end


-- 打印出uniqueLines1中的所有行
for _, line in ipairs(uniqueLines1) do
    print(line)
end

local err = ioutil.write_file("kkbt/diff.txt", md5List)
if err then error(err) end


os.execute('cp kkbt/diff.txt kkbt/diff.old.txt')

